package com.xiaoxin.experience.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author xiaoxin
 */
public class CollectionUtil
{

    private CollectionUtil() {}

    public static boolean isEmpty(Collection<?> collection)
    {
        return collection == null || collection.isEmpty();
    }

    public static boolean isEmpty(Map<?, ?> map)
    {
        return map == null || map.isEmpty();
    }

    public static boolean isNotEmpty(Map<?, ?> map)
    {
        return !isEmpty(map);
    }

    public static boolean isNotEmpty(Collection<?> collection)
    {
        return !isEmpty(collection);
    }

    public static <T> List<List<T>> split(List<T> list, int size)
    {
        if (isEmpty(list) || size <= 0)
        {
            return new ArrayList<>();
        }
        return Stream.iterate(0, n -> n + 1).limit((list.size() + size - 1) / size).
                map(serialNumber -> list.stream().skip((long) serialNumber * size).limit(size).collect(Collectors.toList())).
                collect(Collectors.toList());
    }
}
